/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.basic.park.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.basic.common.constant.ParkDicConstant;
import com.hopes.bizz.basic.common.constant.enums.ParkBasicSysDictEnum;
import com.hopes.bizz.basic.park.dto.BaseAccidentEventExcelDTO;
import com.hopes.bizz.basic.park.entity.BaseAccidentEventEntity;
import com.hopes.bizz.basic.park.mapper.BaseAccidentEventMapper;
import com.hopes.bizz.basic.park.service.BaseAccidentEventService;
import com.hopes.bizz.basic.park.vo.AccidentEventGisVo;
import com.hopes.bizz.basic.park.vo.BaseAccidentEventExcelVO;
import com.hopes.bizz.common.core.validate.ValidatorUtils;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.admin.api.resolver.enums.AdminDictEnum;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 企业事故事件
 *
 * @author pig
 * @date 2023-10-25 14:43:06
 */
@Service
public class BaseAccidentEventServiceImpl extends ServiceImpl<BaseAccidentEventMapper, BaseAccidentEventEntity> implements BaseAccidentEventService {

    @Override
    public ValidateImportBO<BaseAccidentEventExcelDTO> validateImport(List<BaseAccidentEventExcelDTO> excelDTOList) {
        ValidateImportBO<BaseAccidentEventExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<BaseAccidentEventExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;
        for (BaseAccidentEventExcelDTO baseAccidentEventExcelDTO : excelDTOList) {
            String validateRes = ValidatorUtils.validateEntity(baseAccidentEventExcelDTO);
            if (StrUtil.isEmpty(validateRes)) {
                successNum++;
                saveList.add(baseAccidentEventExcelDTO);
            } else {
                failedNum++;
                errorMessageList.add(new ErrorMessage(baseAccidentEventExcelDTO.getLineNum(), new HashSet<String>() {{
                    add(validateRes);
                }}));
            }
        }
        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public BaseAccidentEventEntity importDtoToEntity(BaseAccidentEventExcelDTO excelDto) {
        BaseAccidentEventEntity entity = new BaseAccidentEventEntity();
        BeanUtils.copyProperties(excelDto, entity);
        entity.setOrgId(Long.valueOf(DictResolver.getItemValueByLabel(AdminDictEnum.sys_company, excelDto.getOrgId())));
        entity.setAccidentSource(DictResolver.getItemValueByLabel(ParkBasicSysDictEnum.accident_source, excelDto.getAccidentSource()));
        entity.setAccidentLevel(DictResolver.getItemValueByLabel(ParkBasicSysDictEnum.accident_level, excelDto.getAccidentLevel()));
        entity.setAccidentType(DictResolver.getItemValueByLabel(ParkBasicSysDictEnum.accident_type, excelDto.getAccidentType()));
        entity.setAccidentStatus(DictResolver.getItemValueByLabel(ParkBasicSysDictEnum.accident_status, excelDto.getAccidentStatus()));
        return entity;
    }

    @Override
    public BaseAccidentEventExcelVO exportEntityToVo(BaseAccidentEventEntity entity) {
        BaseAccidentEventExcelVO excelVO = new BaseAccidentEventExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        return excelVO;
    }

    @Override
    public AccidentEventGisVo getAccidentEvent() {
        AccidentEventGisVo accidentEventGisVo = new AccidentEventGisVo();
        Date now = new Date();
        List<BaseAccidentEventEntity> accidentEventEntities = baseMapper.queryNewestByLevel();
        // 计算距上次事故间隔的天数
        Map<String, String> timeMap = accidentEventEntities.stream().collect(Collectors.toMap(BaseAccidentEventEntity::getAccidentLevel, item -> {
            return "无"+DictResolver.getItemLabelByValue(ParkBasicSysDictEnum.accident_level,item.getAccidentLevel())
                    +"第"+DateUtil.betweenDay(now, item.getAccidentTime(), true)+"天";
        }));
        accidentEventGisVo.setSignificantEvent(timeMap.getOrDefault(ParkDicConstant.SIGNIFICANT_EVENT,"无特别重大事故发生"));
        accidentEventGisVo.setCommonEvent(timeMap.getOrDefault(ParkDicConstant.COMMON_EVENT,"无重大事故发生"));
        accidentEventGisVo.setImportantEvent(timeMap.getOrDefault(ParkDicConstant.IMPORTANT_EVENT,"无较大事故发生"));
        accidentEventGisVo.setLargerEvent(timeMap.getOrDefault(ParkDicConstant.LARGER_EVENT,"无一般事故发生"));
        return accidentEventGisVo;
    }
}
